﻿using System.Text;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace CometDocs
{
    internal partial class Client
    {
        /// <summary>
        /// Get all the conversions of a file or all file if none is provided.
        /// </summary>
        /// <param name="token">A valid authentication token.</param>
        /// <param name="file">A file.</param>
        /// <returns>An array of conversions.</returns>
        public Conversion[] GetConversions(AuthenticationToken token, FileInfo file = null)
        {
            NameValueCollection @params = new NameValueCollection(2);
            @params.Add("token", token.Value);
            if (file != null)
            {
                @params.Add("file", file.ID.ToString());
            }

            byte[] result = webClient.UploadValues(APIRoot + "getConversions", @params);

            string json = Encoding.ASCII.GetString(result);

            GetConversionsResponse response = JsonConvert.DeserializeObject<GetConversionsResponse>(json);

            CheckAndThrow(response);

            return response.Conversions;
        }
    }
}
